import Vue from "vue";
import Vuex from "vuex";
import {
  getMenuList,
  getStuList,
  delStu,
  getLoginlog,
  updateStu,
  addStu,
  searchStu,
  getClassList
} from "../api/index";
import allRoutes from "../router/allRoutes";
import recursionRoutes from "../utils/recursionRoutes";
import dynamicRoutes from "../router/dynamicRoutes";
import router from "../router";

Vue.use(Vuex);

//刷新页面会丢失state.userinfo的数据，所以要从localstorage里取
let userInfo = localStorage.getItem("stu-userinfo") || "{}";
//刷新页面会丢失state.permission的数据，所以要从localstorage里取
let permission = localStorage.getItem("stu-permission-buttons") || "[]"
userInfo = JSON.parse(userInfo);
permission = JSON.parse(permission)
export default new Vuex.Store({
  state: {
    userInfo,
    permission,
    MenuList: [],
    Loginlog: [],
    tableData: [],
    crumlist: [],
    searchlist: [],
    classList:[],
  },
  mutations: {
    SET_USERINFO(state, payload) {
      state.userInfo = payload;
    },
    SET_MENULIST(state, payload) {
      state.MenuList = payload;
      //找到dynamicRoutes中path为"/"的选项，将state.MenuList赋值给children
      let target = dynamicRoutes.find(item => {
        return item.path === "/";
      });
      target.children = state.MenuList;
      //将dynamicRoutes路由添加到router下index.js文件中，核心方法router.addRoutes(符合路由配置规则的数据)
      router.addRoutes(dynamicRoutes);
    },
    SET_LOGINLOG(state, payload) {
      state.Loginlog = payload;
    },
    //存储按钮权限
    SET_PERMISSION(state,payload){
      state.permission=JSON.parse(localStorage.getItem("stu-permission-buttons"))
    },
    SET_GETCLASSLIST(state,payload){
        let arr=payload
        state.classList=[]
        for(let i of arr){
          let obj={
            value:i,
            label:i
          }
          state.classList.push(obj)
        }
    },
    SET_STULIST(state, payload) {
      state.tableData = payload;
    },
    SET_DELSTU(state, payload) {
      let res = state.tableData.find(item => {
        return item.sId == payload;
      });
      let index = state.tableData.indexOf(res);
      state.tableData.splice(index, 1);
    },
    SET_ADDSTU(state, payload) {
      state.tableData.push(payload);
    },
    SET_UPDATESTU(state, payload) {
      state.tableData = state.tableData.map(item => {
        return item.sId == payload.sId ? payload : item;
      });
    },
    //更改crumlist面包屑状态
    SET_CRUMLIST(state, payload) {
      state.crumlist = payload;
    },
    SET_SEARCH(state, payload) {
      state.searchlist = payload;
      // let target=state.tableData
      // target.map((item)=>{return item.sId == payload.sId?payload:item})
      console.log(state.searchlist);
    }
  },
  actions: {
    //发送请求获取后台menulist
    async FETCH_MENULIST({ commit }) {
      //接收后台menulist
      let res = await getMenuList();
      //将allRoutes和menulist放入递归函数，接收结果
      let userRoutes = recursionRoutes(allRoutes, res.data.menuList);
      //将递归函数处理结果送给SET_MENULIST
      commit("SET_MENULIST", userRoutes);
    },
    //获取登陆日志
    FETCH_GETLOGINLOG({ commit }) {
      getLoginlog()
        .then(res => {
          if (res.data.state) {
            commit("SET_LOGINLOG", res.data.data);
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    //获取按钮权限并存储
    FETCH_PERMISSION({commit}){
      commit("SET_PERMISSION")
    },
    //获取学员信息
    FETCH_GETSTULIST({ commit }) {
      getStuList()
        .then(res => {
          commit("SET_STULIST", res.data.data);
        })
        .catch(err => {
          console.log(err);
        });
    },
    //获取班级信息
    FETCH_GETCLASSLIST({commit}){
      getClassList()
      .then(res=>{
        if(res.data.state){
          commit("SET_GETCLASSLIST",res.data.data)
        }
      })
      .catch(err=>{
        console.log(err)
      })
    },
    //删除学员
    FETCH_DELSTU({ commit }, payload) {
      delStu(payload)
        .then(res => {
          if (res.data.state) {
            commit("SET_DELSTU", payload);
            console.log("删除成功");
          } else {
            console.log("删除出错");
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    //修改学员信息
    FETCH_UPDATESTU({ commit }, payload) {
      updateStu(payload)
        .then(res => {
          console.log(res);
          if (res.data.state) {
            commit("SET_UPDATESTU", payload);
            console.log("修改成功");
          }
        })
        .catch(err => {
          console.log(err);
        });
    },
    //添加学员信息
    FETCH_ADDSTU({ commit }, payload) {
      console.log(payload)
      addStu(payload).then(res => {
        if (res.data.state) {
          commit("SET_ADDSTU", payload);
        }
      });
    },
    //搜索学员信息
    FETCH_SEARCH({ commit }, payload) {
      searchStu(payload)
        .then(res => {
          if (res.data.state) {
            commit("SET_SEARCH", res.data.data);
          }
        })
        .catch(err => {
          console.log(err);
        });
    }
  },
  getters:{
    isadd(state){
      if(state.permission.includes("add")){
        return true
      }else{
        return false
      }
    },
    isedit(state){
      if(state.permission.includes("edit")){
        return true
      }else{
        return false
      }
    },
    isdel(state){
      if(state.permission.includes("delete")){
        return true
      }else{
        return false
      }
    }
  },
  modules: {}
});
